package com.amc.controllers;

import java.io.IOException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.amc.configs.Config;
import com.amc.dto.ComplaintView;
import com.amc.dto.FoodView;
import com.amc.dto.OrderView;
import com.amc.service.IOrderService;
import com.amc.utils.ResJsonUtil;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;

@Controller
@RequestMapping("/order")
public class OrderContoller {
	private IOrderService service;

	@Resource(name = "orderService")
	public void setService(IOrderService service) {
		this.service = service;
	}

	@RequestMapping(value = "/orderlist", method = { RequestMethod.GET })
	@ResponseBody
	public List<OrderView> initOrder(String customerId) {
		return service.queryAllOrder(customerId);
	}

	@RequestMapping(value = "/detail", method = { RequestMethod.GET })
	@ResponseBody
	public OrderView orderDetail(String orderId) {
		return service.queryOrderById(orderId);
	}

	@RequestMapping(value = "/placeorder", method = { RequestMethod.POST })
	@ResponseBody
	public Object placeOrder(String addressId, String customerId,
			String merchantId, String orderFoods) throws JsonParseException,
			JsonMappingException, IOException {

		ObjectMapper mapper = new ObjectMapper();
		JavaType javaType = mapper.getTypeFactory().constructParametricType(
				List.class, FoodView.class);
		List<FoodView> list = mapper.readValue(orderFoods, javaType);

		OrderView ov = new OrderView();
		ov.setAddressId(addressId);
		ov.setCustomerId(customerId);
		ov.setMerchantId(merchantId);
		ov.setOrderFoods(list);

		boolean result = service.placeOrder(ov);
		return result == true ? ResJsonUtil.responseObject(Config.SUCCESS, "",
				"") : ResJsonUtil.responseObject(Config.ERROR, "",
				"Place Order Fail");
	}

	@RequestMapping(value = "/cancelorder", method = { RequestMethod.GET })
	@ResponseBody
	public Object cancleOrder(String orderId) {
		boolean result = service.refuseOrder(orderId);
		return result == true ? ResJsonUtil.responseObject(Config.SUCCESS, "",
				"") : ResJsonUtil.responseObject(Config.ERROR, "",
				"Cancel Order Fail");
	}

	@RequestMapping(value = "/complaint", method = { RequestMethod.POST })
	@ResponseBody
	public Object complaint(ComplaintView complaint) {
		System.out.println("WAHT: " + complaint);
		if (service.complaintMerchant(complaint)) {
			return ResJsonUtil.responseObject(Config.SUCCESS, "", "");
		}
		return ResJsonUtil.responseObject(Config.ERROR, "", "Complaint Fail");
	}

	@RequestMapping(value = "/confirmorder", method = { RequestMethod.GET })
	@ResponseBody
	public Object comfirmOrder(String orderId) {
		if (service.receiptOrder(orderId)) {
			return ResJsonUtil.responseObject(Config.SUCCESS, "", "");
		} else {
			return ResJsonUtil.responseObject(Config.ERROR, "", "Comfirm Fail");
		}
	}

}
